//
//  CalendarCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/16.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "CalendarCell.h"
#import "FSCalendar.h"
#import "NSDate+Extension.h"

@interface CalendarCell()<FSCalendarDataSource,FSCalendarDelegate,FSCalendarDelegateAppearance>

@property (weak, nonatomic) IBOutlet FSCalendar *calendar;
@property (strong, nonatomic) NSCalendar *gregorian;

@property (strong, nonatomic) NSDateFormatter *dateFormatter;
@property (nonatomic,strong) NSMutableDictionary *completes;//完成为红色
@property (nonatomic,strong) NSMutableDictionary *unCompletes;//未完成为红色

@end

@implementation CalendarCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.calendar.dataSource = self;
    self.calendar.delegate = self;
    
    self.dateFormatter = [[NSDateFormatter alloc] init];
    self.dateFormatter.dateFormat = @"yyyy-MM-dd";
    
    self.gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    self.calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase;
    //隐藏两边的年月
    self.calendar.appearance.headerMinimumDissolvedAlpha = 0;
    self.calendar.appearance.titleFont = FontB(16);
    self.calendar.appearance.weekdayTextColor = [UIColor grayColor];
    self.calendar.appearance.headerTitleColor = [UIColor grayColor];
    self.calendar.appearance.headerDateFormat = @"yyyy年MM月";
    self.calendar.appearance.todayColor = RGBColorOne(155);
    self.calendar.appearance.separators = FSCalendarSeparatorInterRows;
//    self.calendar.placeholderType = FSCalendarPlaceholderTypeNone;
//    self.calendar.allowsSelection = NO;
}

#pragma mark - <FSCalendarDelegate>
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    NSString *key = [date convertDateToSting];
    if ([[self.completes allKeys] containsObject:key]) {
        if (self.selectBlock) {
            self.selectBlock(self.completes[key]);
        };
    }else if ([[self.unCompletes allKeys] containsObject:key]) {
        if (self.selectBlock) {
            self.selectBlock(self.unCompletes[key]);
        };
    }
    return NO;
}
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    
}

#pragma mark - <FSCalendarDelegateAppearance>
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance fillDefaultColorForDate:(NSDate *)date
{
    NSString *key = [self.dateFormatter stringFromDate:date];
    if ([self.completes.allKeys containsObject:key]) {
        ClubActivityListModel *model = self.completes[key];
        return model.color;
    }else if ([self.unCompletes.allKeys containsObject:key]) {
        ClubActivityListModel *model = self.unCompletes[key];
        return model.color;
    }
    return nil;
}
//当前月份变化时 滑动或点击时
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar
{
    [self.completes removeAllObjects];
    [self.unCompletes removeAllObjects];
    if (self.pageChanged) {
        self.pageChanged(calendar.currentPage);
    }
}

- (void)setSources:(NSArray *)sources
{
    _sources = sources;
    if (sources.count > 0) {
        //拆分数据
        for (ClubActivityListModel *model in sources) {
            NSDate *date = [NSDate spToDate:model.startDate];
            NSDateComponents *cmp = [date compareToDate:[NSDate date]];
            NSString *key = [date convertDateToSting];
            //cmp.day正值为过去  负值为将来
            if (cmp.year > 0 || cmp.month > 0 ||cmp.day > 0) {
                model.color = [UIColor redColor];
                [self.completes setValue:model forKey:key];
            }else {
                model.color = [UIColor greenColor];
                [self.unCompletes setValue:model forKey:key];
            }
        }
    }
    [self.calendar reloadData];
}

- (IBAction)preNextClick:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
        {
            NSDate *currentMonth = self.calendar.currentPage;
            NSDate *previousMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:currentMonth options:0];
            [self.calendar setCurrentPage:previousMonth animated:YES];
        }
            break;
        case 1:
        {
            NSDate *currentMonth = self.calendar.currentPage;
            NSDate *nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:currentMonth options:0];
            [self.calendar setCurrentPage:nextMonth animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (NSMutableDictionary *)completes
{
    if (!_completes) {
        _completes = [NSMutableDictionary dictionary];
    }
    return _completes;
}
- (NSMutableDictionary *)unCompletes
{
    if (!_unCompletes) {
        _unCompletes = [NSMutableDictionary dictionary];
    }
    return _unCompletes;
}

@end
